<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('SAP入库单')"/>
    <style>
        [v-cloak] {
            display: none;
        }
        .el-date-editor.el-input, .el-date-editor.el-input__inner {
            width: 200px!important;
        }
        .el-input__icon {
            line-height: 0!important;
        }
        .tag {
            display: inline-block;
            padding: 0 5px;
            line-height: 22px;
            color: #FFFFFF;
            border-radius: 3px;
        }
        .disabled{
            pointer-events: none;
            cursor: not-allowed !important;
            background: #ccc !important;
        }
    </style>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li v-if="checkedSearch.indexOf('外部交货单号') != -1">
                            外部交货单号：<input type="text" id="vbeln" name="sapNo"/>
                        </li>
                        <li v-if="checkedSearch.indexOf('入库类型') != -1">
                            入库类型：
                            <select name="type">
                                <option value="">全部</option>
                                <option value="1">采购入库</option>
<!--                                <option value="2">生产退料</option>-->
<!--                                <option value="3">采购预收</option>-->
<!--                                <option value="4">试做品入库</option>-->
                                <option value="5">采购预收</option>
                            </select>
                        </li>
                        <li v-if="checkedSearch.indexOf('创建人') != -1">
                               创建人：<input type="text" id="createName" name="createName"/>
                        </li>
                        <li v-if="checkedSearch.indexOf('过账状态') != -1">
                            过账状态：
                            <select name="state">
                                <option value="">全部</option>
                                <option value="1">未过账</option>
                                <option value="2">已过账</option>
                                <option value="3">部分过账</option>
                                <option value="4">入库完成</option>
                            </select>
                        </li>
                        <li v-if="checkedSearch.indexOf('单据状态') != -1">
                            单据状态：
                            <select name="billState">
                                <option value="">全部</option>
                                <option value="1">待审核</option>
                                <option value="2">已审核</option>
<!--                                <option value="3">处理中</option>-->
<!--                                <option value="4">已完成</option>-->
                            </select>
                        </li>
                        <li v-show="checkedSearch.indexOf('创建时间') != -1">
                            <label>创建时间：</label>
                            <el-date-picker
                                name="startTime"
                                v-model="timeA"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                type="datetime"
                                @change="compareAB"
                                placeholder="开始时间">
                            </el-date-picker>
                            <span>-</span>
                            <el-date-picker
                                name="endTime"
                                v-model="timeB"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                type="datetime"
                                @change="compareAB"
                                placeholder="结束时间">
                            </el-date-picker>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()" @click="resetTime()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            <a class="btn btn-success btn-rounded btn-sm" @click="moreSearch()"><i class="fa fa-edit"></i>&nbsp;更多</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <el-dialog title="搜索条件" width="215px" :visible.sync="showMore">
            <el-row>
                <el-col :span="24">
                    <el-checkbox style="margin-left: 28px" :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
                    <el-checkbox-group v-model="checkedSearch" @change="handleCheckedSearchChange">
                        <el-checkbox style="display: block;margin-left: 28px" v-for="item in searches" :label="item" :key="item" v-cloak>{{item}}</el-checkbox>
                    </el-checkbox-group>
                </el-col>
            </el-row>
        </el-dialog>
        <div class="btn-group-sm" id="toolbar" role="group">
<!--            <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="in:billInSap:exportTake">-->
<!--                <i class="fa fa-download"></i> 导出全部-->
<!--            </a>-->
<!--            <a class="btn btn-info" @click="partExcel()" shiro:hasPermission="in:billInSap:partExcelTake">-->
<!--                <i class="fa fa-download"></i> 部分导出-->
<!--            </a>-->
            <a class="btn btn-info" onclick="$.table.importExcel()">
                <i class="fa fa-upload"></i> 导入
            </a>
<!--            <a class="btn btn-primary" @click="bit()">-->
<!--                <i class="fa fa-print"></i> 打印-->
<!--            </a>-->
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table" data-mobile-responsive="true" style="white-space: nowrap"></table>
        </div>
    </div>
</div>
<div th:include="include :: footer"></div>
<script th:src="@{/wms/js/moreSearch.js}"></script>
<script th:inline="javascript">
    var prefix = ctx + "in/billInSap";

    const searchOptions = ['外部交货单号', '入库类型', '单据状态', '过账状态', '创建人', '创建时间'];

    var app = new Vue({
        el: '.container-div',
        data: {
            showMore: false,
            checkAll: false,
            checkedSearch: ['外部交货单号', '入库类型', '单据状态', '创建人'],
            searches: searchOptions,
            isIndeterminate: true,
            timeA: '',
            timeB: '',
        },
        methods: {
            //时间对比
            compareAB: function(){
                if(this.timeA != '' && this.timeA != null){
                    if(this.timeB != '' && this.timeB != null){
                        var a = new Date(this.timeA);
                        var b = new Date(this.timeB);
                        if(a >= b){
                            $.modal.alertError("结束时间需大于开始时间，请重新选择！！");
                            this.timeA = '';
                            this.timeB = '';
                        }
                    }else{
                        this.timeB = '';
                    }
                }else{
                    this.timeA = '';
                    if(this.timeB == null){
                        this.timeB = '';
                    }
                }
            },
            //清空时间
            resetTime(){
                this.timeA = '';
                this.timeB = '';
            },
            //部分导出
            partExcel(){
                var rows = $.common.isEmpty($.table._option.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns($.table._option.uniqueId);
                if(rows.length == 0){
                    $.modal.alertWarning("请至少选择一条记录");
                    return;
                }else{
                    $.modal.confirm("确认要导出选中的" + rows.length + "条数据吗?", function(){
                        var url = prefix + "/partExcelTake";
                        var data = {"ids": rows.join()};
                        $.post(url, data, function(result){
                            if(result.code == web_status.SUCCESS){
                                window.location.href = ctx + "common/download?fileName=" + encodeURI(result.msg) + "&delete=" + true;
                            }else if(result.code == web_status.WARNING){
                                $.modal.alertWarning(result.msg);
                            }else{
                                $.modal.alertError(result.msg);
                            }
                            $.modal.closeLoading();
                        });
                    });
                }
            },
            //打印按钮
            bit(){
                var rows = $.common.isEmpty($.table._option.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns($.table._option.uniqueId);
                if(rows.length == 0){
                    $.modal.alertWarning("请至少选择一条记录");
                    return;
                }else{
                    $.modal.openTab("打印", "http://localhost:8000/in/billInSap/printst?ids="+rows);
                }
            }
        }
    })

    $(function () {
        var options = {
            url: prefix + "/list",
            exportUrl: prefix + "/exportTake",
            importUrl: prefix + "/importData",
            importTemplateUrl: prefix + "/importTemplate",
            detailUrl: prefix + "/billInSapTakeDetail",
            auditUrl: prefix + "/billInSapAudit",
            modalName: "SAP收货单",
            columns: [
                {
                    checkbox: true
                },
                {
                    field: 'billInSapId',
                    title: '序号',
                    align: 'center',
                    visible: false
                },
                {
                    field: 'sapNo',
                    title: '外部交货单号',
                    align: 'center'
                },
                {
                    field: 'state',
                    title: '过账状态',
                    align: 'center',
                    formatter: function(value, row, index){
                        var actions = [];
                        if(value == 1){
                            actions.push("<span class='tag' style='background-color: #fcb529'>未过账</span>");
                        }else if(value == 2){
                            actions.push("<span class='tag' style='background-color: #45ade1'>已过帐</span>");
                        }else if(value == 3){
                            actions.push("<span class='tag' style='background-color: #f8ac59'>部分过账</span>");
                        }else if(value == 4){
                            actions.push("<span class='tag' style='background-color: #f8ac59'>入库完成</span>");
                        }
                        return actions.join('');
                    }
                },
                {
                    field: 'billState',
                    title: '单据状态',
                    align: 'center',
                    formatter: function(value, row, index){
                        var actions = [];
                        if(value == '1'){
                            actions.push("<span class='tag' style='background-color: #fcb529'>待审核</span>");
                        }else if(value == '2'){
                            actions.push("<span class='tag' style='background-color: #45ade1'>已审核</span>");
                        }
                        // else if(value == '3'){
                        //     actions.push("<span class='tag' style='background-color: #f8ac59'>处理中</span>");
                        // }else if(value == '4'){
                        //     actions.push("<span class='tag' style='background-color: #fcb529'>已完成</span>");
                        // }
                        return actions.join('');
                    }
                },
                {
                    field: 'type',
                    title: '入库类型',
                    align: 'center',
                    formatter: function(value, row, index){
                        var actions = [];
                        if(value == '1'){
                            actions.push("<span class='tag' style='background-color: #fcb529'>采购入库</span>");
                        }
                        else if(value == '2'){
                            actions.push("<span class='tag' style='background-color: #45ade1'>生产退料</span>");
                        }
                        else if(value == '5'){
                            actions.push("<span class='tag' style='background-color: #f8ac59'>采购预收</span>");
                        }
                        return actions.join('');
                    }
                },
                // {
                //     field: 'supplierName',
                //     title: '供应商名称',
                //     align: 'center'
                // },
                {
                    field: 'createName',
                    title: '创建人',
                    align: 'center'
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    align: 'center',
                    formatter: function(value, item, index){
                        if(value == null){
                            return '';
                        }else{
                            return value.slice(0, 19);
                        }
                    }
                },
                {
                    field: 'bldat',
                    title: '凭证日期',
                    align: 'center',
                    formatter: function(value, item, index){
                        if(value == null){
                            return '';
                        }else{
                            return value.slice(0, 19);
                        }
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    events: {
                        'click .detail': function(e, value, rowData, index){
                            var id = rowData.billInSapId;
                            var _url = $.table._option.detailUrl;
                            localStorage.setItem(_url, id.toString());
                            $.modal.openFull("单据明细", _url);
                        },
                        'click .audit': function(e, value, rowData, index){
                            var id = rowData.billInSapId;
                            var _url = $.table._option.auditUrl;
                            localStorage.setItem(_url, id.toString());
                            $.modal.openFull("审核", _url);
                        },
                        'click .del': function(e, value, rowData, index){
                            var data = {
                                id: rowData.billInSapId
                            };
                            $.modal.confirm("确认要删除吗?", function(){
                                var url = prefix + "/detele";
                                $.ajax({
                                    cache: true,
                                    type: "POST",
                                    url: url,
                                    data: data,
                                    async: false,
                                    error: function(data){
                                        $.modal.alertError("系统错误");
                                    },
                                    success: function(data){
                                        if(data.code == 0){
                                            $.modal.msgSuccess('已完成');
                                            $.table.refresh();
                                        }else{
                                            $.modal.alertError(data.msg);
                                        }
                                    }
                                });
                            })
                        },
                        'click .post': function(e, value, rowData, index) {
                            $.modal.confirm("确认要强制过账吗?", function(){
                                console.log(rowData);
                                var url = prefix + "/sapPosting";
                                $.ajax({
                                    cache: true,
                                    type: "POST",
                                    url: url,
                                    dataType: "json",
                                    contentType: "application/json;charset-UTF-8",
                                    data: JSON.stringify(rowData),
                                    async: false,
                                    error: function(request){
                                        $.modal.alertError("系统错误");
                                    },
                                    success: function(result){
                                        console.log(result)
                                        if(result.code == web_status.SUCCESS){
                                            window.location.href = ctx + "common/download?fileName=" + encodeURI(result.msg) + "&delete=" + true;
                                        }else if(result.code == web_status.WARNING){
                                            $.modal.alertWarning(result.msg);
                                        }else{
                                            $.modal.alertError(result.msg);
                                        }
                                        $.modal.closeLoading();
                                    }
                                });
                            });
                        }
                    },
                    formatter: function(value, row, index){
                        var actions = [];
                        var editFlag = 'disabled';
                        var auditFlag = 'disabled';
                        if(row.state != 2 && row.billState == 2){
                            editFlag = '';
                        }
                        if(row.billState == 1){
                            auditFlag = '';
                        }
                        actions.push("<span class='detail tag' style='background-color: #3097b5;cursor: pointer'><i class='fa fa-hand-pointer-o'></i> 查看</span>");
                        actions.push('<span class="post tag ' + editFlag + '" style="background-color: #3097b5;cursor: pointer; margin-left: 10px"><i class="fa fa-hand-pointer-o"></i> 强制过账</span>');
                        actions.push('<span class="audit tag ' + auditFlag + '" style="background-color: #3097b5;cursor: pointer; margin-left: 10px"><i class="fa fa-hand-pointer-o"></i> 审核</span>');
                        actions.push('<span class="del tag ' + auditFlag + '" style="background-color: #3097b5;cursor: pointer; margin-left: 10px"><i class="fa fa-hand-pointer-o"></i> 删除</span>');
                        return actions.join('');
                    }
                }
            ]
        };
        $.table.init(options);
    });

    function open(title, url, width, height, callback){
        if(navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)){
            width = 'auto';
            height = 'auto';
        }
        if($.common.isEmpty(title)){
            title = false;
        }
        if($.common.isEmpty(url)){
            url = "/404.html";
        }
        if($.common.isEmpty(width)){
            width = 800;
        }
        if($.common.isEmpty(height)){
            height = ($(window).height() - 250);
        }
        if($.common.isEmpty(callback)){
            callback = function(index, layero){
                var iframeWin = layero.find('iframe')[0];
                iframeWin.contentWindow.submitHandler();
            }
        }
        if (title == '审核') {
            var _btn = ['关闭', '审核'];
        } else {
            var _btn = ['关闭'];
        }
        layer.open({
            type: 2,
            area: [width + 'px', height + 'px'],
            fix: false,
            maxmin: true,
            shade: 0.3,
            title: title,
            content: url,
            btn: _btn,
            shadeClose: true,
            cancel: function(index){
                return true;
            }
            ,btn2: function(){
                console.log(111)
            }
        });
    }
</script>
</body>
<form id="importForm" enctype="multipart/form-data" class="mt20 mb10" style="display: none">
    <div class="col-xs-offset-1">
        <input type="file" id="file" name="file"/>
        <div class="mt10 pt5">
            <input type="checkbox" id="updateSupport" name="updateSupport" title="如果SAP数据已经存在，更新这条数据。">
            是否更新已经存在的SAP数据
            <a onclick="$.table.importTemplate()" class="btn btn-default btn-xs"><i class="fa fa-file-excel-o"></i> 下载模板</a>
        </div>
        <font color="red" class="pull-left mt10">
            提示：仅允许导入“xls”或“xlsx”格式文件！
        </font>
    </div>
</form>
</html>
